import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView.vue'
import LayOutView from '../views/LayOutView.vue'
import RegisterView from '../views/RegisterView.vue'
import BookView from '../views/BookView.vue'
import BookType from '../views/BookType.vue'

Vue.use(VueRouter)

const routes = [
 
  {
    path: '/login',
    name: 'login',
    component: LoginView
  },
  {
    path: '/register',
    name: 'register',
    component: RegisterView
  },
  {
    path: '/',
    name: 'outView',
    component: LayOutView,
    children:[
      {
        path: '',
        name: 'home',
        component: HomeView
      },
      {
        path: '/admin',
        name: 'admin',
        component: () => import( '../views/AdminInfo.vue')
      },
      {
        path: '/books',
        name: 'books',
        component: BookView
      },
      {
        path: '/type',
        name: 'type',
        component: BookType
      }
    ]
  }  
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//路由守卫
router.beforeEach((to,from,next)=>{
  if(to.path==='/login'){
    next();
  }
  const user=localStorage.getItem("user");
  if(!user&&to.path!=='/login'){
    return next("/login");
  }
  next()
})
export default router
